<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部工具条 -->
        <tree-tools :is-root="true" :tree-node="company" @addDepts="addDepts" />
        <!-- 树形控件 -->
        <!-- data是树形控件的数据 -->
        <!-- props是树形控件的展示规则 -->
        <el-tree :data="departs" :props="{ label:'name' }">
          <tree-tools slot-scope="{ node, data }" :tree-jiedian="node" :tree-node="data" @addDepts="addDepts" @delDepts="getDepartments" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <addDept ref="add-depts" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    addDept
  },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [],
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' } // 这里定义一个空串  因为 它是根 所有的子节点的数据pid 都是 ""
      this.departs = tranListToTreeData(result.depts, '')
      console.log(result)
    },
    addDepts(val) {
      this.node = val
      this.$refs['add-depts'].isShow = true
    },
    async editDepts(val) {
      console.log(val)
      this.node = val
      await this.$refs['add-depts'].getDepartDetail(val.id)
      this.$refs['add-depts'].isShow = true
    }
  }
}
</script>
<style scoped lang="scss">
// 在scss中使用的是  ::v-deep
// 在less中使用的是  /deep/
::v-deep .tree-card {
  padding: 30px  140px;
  font-size:14px;
  .el-tree-node__expand-icon{
    color: #fff;
  }
  .el-tree-node__content:hover .el-tree-node__expand-icon{
    color: #F5F7FA;
  }
}
</style>
